﻿// -----------------------------------------------------------------------
// <copyright file="TestExceptionHelper.cs" company="Companytec GmbH">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Reflection;
using Company.Common;
using Company.Common.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Company.System.IO.IntegrationTests
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class TestExceptionHelper<TException>
    {
        public static void Expect<TObject>(bool innerException, TObject objectInstance, string methodName,
                                           Type[] parameterTypes, object[] parameterValues)
        {
            Expect(innerException, objectInstance, typeof (TException), methodName, parameterTypes, parameterValues);
        }

        private static void Expect<TObject>(bool innerException, TObject objectInstance, Type exceptionType,
                                            string methodName, Type[] parameterTypes, object[] parameterValues)
        {
            MethodInfo methodInfo = GetMethodInfo<TObject>(methodName, parameterTypes);
            CompanyContract.ThrowIfNull(methodInfo, "MethodInfo is null.");

            try
            {
                methodInfo.Invoke(objectInstance, parameterValues);
            }
            catch (global::System.Exception exception)
            {
                Assert.IsInstanceOfType(innerException ? exception.InnerException : exception, exceptionType,
                                        string.Format("{0}", methodInfo));
            }
        }

        private static MethodInfo GetMethodInfo<TObject>(string methodName, Type[] parameterTypes)
        {
            Type objectType = typeof (TObject);
            return objectType.GetMethod(methodName, parameterTypes);
        }
    }
}